<?php
namespace app\login\controller;
use think\Controller;
use think\Db;
// use app\login\controller\PayReturn;

class Alipay extends Controller
{
    // 支付宝 appid
    public $appid = '2018101761649906';
    //支付宝公钥
    public $alipayrsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm4exod41nOgIc1k9I48ImVgkxplp27eDfnbab852QcOrqmEeXkNIYqGnSo4BrsfofzbYmAau3WKqSbtOzTYdTpAv2iSEeLqdlXEszzloFHLWzGDBBBjzAyjf7/Hyxlhg2d69PBdA40k6rcXD7DJsw8N20w7Kxi6IIc/jCl1BQAWJDrCYK4fVsR0n0yKqiIbA6bW/dMdEWRa2H3sz6wGCBDO7JV4Ecj/odIjuCU+cmX9QDDF7+Jj+dKPdPdg31zS/UGFhQ0nM6GzLonQVXPo4KoitNkbxhsYZATB2LpGLKUK/BUph0Zk/jFageG6BAS0YMuU3Y8pSzz3n1CJYR802nwIDAQAB';
    //支付宝私钥
    public $rsaPrivateKey = 'MIIEoQIBAAKCAQEAm4exod41nOgIc1k9I48ImVgkxplp27eDfnbab852QcOrqmEeXkNIYqGnSo4BrsfofzbYmAau3WKqSbtOzTYdTpAv2iSEeLqdlXEszzloFHLWzGDBBBjzAyjf7/Hyxlhg2d69PBdA40k6rcXD7DJsw8N20w7Kxi6IIc/jCl1BQAWJDrCYK4fVsR0n0yKqiIbA6bW/dMdEWRa2H3sz6wGCBDO7JV4Ecj/odIjuCU+cmX9QDDF7+Jj+dKPdPdg31zS/UGFhQ0nM6GzLonQVXPo4KoitNkbxhsYZATB2LpGLKUK/BUph0Zk/jFageG6BAS0YMuU3Y8pSzz3n1CJYR802nwIDAQABAoIBAD2ZqARJg4iK6lXtYWbFDw3VOHOkbaYFcEJVhrzD1FjFzCcmYiXJllDiX+UKi6YTLYW9TlogQ4ARxQqA8nwa0867Ewjeb811dFatRacpwIxg06yNIcWisBVvSgq6FIL0nB+Otfqn5x5ecaxD6Q6TBjsfHuAHOs71vRtPQEhpmR9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宝同步通知
    // public function initialize(){}
    public function appAlipay($body,$serial_number,$totalFee,$notifyUrl)
    {

         include_once EXTEND_PATH.'alipay/aop/AopClient.php';
         include_once EXTEND_PATH.'alipay/aop/request/AlipayTradeAppPayRequest.php';
        // 时间戳
        $aop           = new \AopClient();
        $bizcontent    = json_encode([
            'body'         => $body, //商品名称
            'subject'      => $body, //主题
            'out_trade_no' => $serial_number, //此订单号为商户唯一订单号
            'total_amount' => $totalFee, //保留两位小数
            'product_code' => 'QUICK_MSECURITY_PAY',
        ]);

        //支付宝开始
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        //实际上线app id需真实的
        $aop->appId              = $this->appid;
        $aop->rsaPrivateKey      = $this->rsaPrivateKey;
        $aop->format             = "json";
        $aop->charset            = "UTF-8";
        $aop->signType           = "RSA2";
        $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;

        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        $request = new \AlipayTradeAppPayRequest();
        //支付宝回调
        // $notifyUrl = "http://newyp.3todo.com/index/Aliapppay/aliPayBack";
        $request->setNotifyUrl($notifyUrl);
        $request->setBizContent($bizcontent);
        //这里和普通的接口调用不同，使用的是sdkExecute
        $response = $aop->sdkExecute($request);
        return ['code'=>1,'msg'=>'签名成功','data'=>$response];

    }

    /**
     * 支付宝支付回调
     *
     */
    public function alipayNotify()
    {
        // vendor('alipay.aop.AopClient');
        $data = input('param.');
        if (!empty($data)) {
            // 支付成功
            $number       = $data['out_trade_no']; //订单的id
            $wepay_serial = $data['trade_no'];
            if (!empty($number)) {
                model('PayOrder')->where([['number','eq',$number]])->update(['status'=>2]);
                $order = model('PayOrder')->where([['number','eq',$number]])->find();
                $r = false;
                if($order['item_type'] == 'service') {
                    $r = model('ServiceBuy')->payNotify($order);
                } else if($order['item_type'] == 'video') {
                    $r = model('VideoBuy')->payNotify($order);
                }
                if($r) {
                    die('success');
                } else {
                    die('failue');
                }
                die('failue'.$order['item_type']);
            }
            die('failue'.$number);
        }
        
    }
  

     /**
     * 支付宝退货处理
     * 退货前提是必须支付成功
     * @param type $orderId
     * @return bool
     */
    // public function alipayRefund($out_trade_no, $totalFee)
    // {
    //     vendor('alipay.aop.AopClient');
    //     vendor('alipay.aop.request.AlipayTradeRefundRequest');
    //     vendor('alipay.aop.SignData');
    //     $aop                     = new \AopClient();
    //     $aop->gatewayUrl         = 'https://openapi.alipay.com/gateway.do';
    //     $aop->appId              = $this->appid;
    //     $aop->rsaPrivateKey      = $this->rsaPrivateKey;
    //     $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
    //     $aop->apiVersion         = '1.0';
    //     $aop->signType           = 'RSA2';
    //     $aop->postCharset        = 'UTF-8';
    //     $aop->format             = 'json';
    //     $request                 = new \AlipayTradeRefundRequest();
    //     $wepay_serial            = ''; //支付宝交易号
    //     $totalFee                = 0.01;
    //     $bizcontent              = json_encode([
    //         'out_trade_no'  => $out_trade_no,
    //         'trade_no'      => $wepay_serial,
    //         'refund_amount' => $totalFee,
    //         'refund_reason' => '正常退款',
    //     ]);
    //     $request->setBizContent($bizcontent);
    //     $result       = $aop->execute($request);
    //     $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
    //     $resultCode   = $result->$responseNode->code;
    //     trace($result,'notice');
    //     if (!empty($resultCode) && $resultCode == 10000) {
    //         return true;
    //     } else {
    //         return false;
    //     }
    // }

}
